<script setup lang="ts">
// import XtxCheckbox from "@/components/XtxCheckbox.vue";
import type { GoodsRequestParams } from "@/types/Goods";
import XtxCheckbox from "../../../components/XtxCheckbox.vue";

const emit = defineEmits<{ (e: "onChanged", sortParams: SortParams): void }>();

// 排序对象的类型
type SortParams = Pick<
  GoodsRequestParams,
  "inventory" | "onlyDiscount" | "sortField" | "sortMethod"
>;
// 排序条件对象的初始值
const initialSortParams: SortParams = {
  inventory: false,
  onlyDiscount: false,
  sortField: "",
  sortMethod: "",
};
// 排序条件状态
const sortParams = ref<SortParams>({ ...initialSortParams });
// 更新排序条件
function updateSortParams(params: Partial<SortParams>) {
  // 更新排序条件
  sortParams.value = {
    ...sortParams.value,
    ...params,
  };
  // 判断用户点击的是按照价格排序
  if (params.sortField === "price") {
    sortParams.value = {
      ...sortParams.value,
      // 当用户反复点击按照价格进行排序时 切换升序和降序
      sortMethod: sortParams.value.sortMethod === "desc" ? "asc" : "desc",
    };
    // 如果用户点击的默认排序  按照最新商品排序 。。。。。
  } else if (
    params.sortField === "" ||
    params.sortField === "publishTime" ||
    params.sortField === "orderNum" ||
    params.sortField === "evaluateNum"
  ) {
    sortParams.value = {
      ...sortParams.value,
      sortMethod: "",
    };
  }
  // console.log(sortParams.value);
  // 触发自定义事件 把用户选择的排序条件传递到父组件
  emit("onChanged", sortParams.value);
}
</script>
<template>
  <div class="sub-sort">
    <div class="sort">
      <a
        href="javascript:"
        @click="updateSortParams({ sortField: '' })"
        :class="{ active: sortParams.sortField === '' }"
        >默认排序</a
      >
      <a
        href="javascript:"
        @click="updateSortParams({ sortField: 'publishTime' })"
        :class="{ active: sortParams.sortField === 'publishTime' }"
        >最新商品</a
      >
      <a
        href="javascript:"
        @click="updateSortParams({ sortField: 'orderNum' })"
        :class="{ active: sortParams.sortField === 'orderNum' }"
        >最高人气</a
      >
      <a
        href="javascript:"
        @click="updateSortParams({ sortField: 'evaluateNum' })"
        :class="{ active: sortParams.sortField === 'evaluateNum' }"
        >评论最多</a
      >
      <a
        href="javascript:"
        @click="updateSortParams({ sortField: 'price' })"
        :class="{ active: sortParams.sortField === 'price' }"
      >
        价格排序
        <i
          class="arrow up"
          :class="{ active: sortParams.sortMethod === 'asc' }"
        ></i>
        <i
          class="arrow down"
          :class="{ active: sortParams.sortMethod === 'desc' }"
        ></i>
      </a>
    </div>
    <div class="check">
      <XtxCheckbox
        :checked="sortParams.inventory"
        @onChanged="updateSortParams({ inventory: $event })"
        >仅显示有货商品</XtxCheckbox
      >
      <XtxCheckbox
        :checked="sortParams.onlyDiscount"
        @onChanged="updateSortParams({ onlyDiscount: $event })"
        >仅显示特惠商品</XtxCheckbox
      >
    </div>
  </div>
</template>

<style scoped lang="less">
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: white;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: white;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
